﻿/*  ----------------------------------------------------------------------------
 *  EuphoricNation
 *  ----------------------------------------------------------------------------
 *  EuphoricEngine v1.0 - 2DPhysics Demo
 *  ----------------------------------------------------------------------------
 *  File:       GamePlayScreen.cs
 *  Author:     Shane-PC\Shane
 *  ----------------------------------------------------------------------------
 */

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Factories;
using FarseerGames.FarseerPhysics.Collisions;

using EuphoricEngine;
using EuphoricEngine.Components.Components2D;

namespace Demo_2DPhysics
{
    public class GamePlayScreen : GameScreen
    {
        private PhysicsSimulator _physics;
        private PhysicsSimulatorDebug _physicsDebug;

        private PhysicsSprite _physicalBlock;

        public GamePlayScreen()
        {
        }

        protected override void Load()
        {
            _physics = new PhysicsSimulator(new Vector2(0f, Constants.EARTH_GRAVITAION) * 10);

            _physicsDebug = new PhysicsSimulatorDebug(_physics, Vector2.Zero);
            _physicsDebug.DrawOrder = int.MaxValue;
            this.AddComponent(_physicsDebug);

            this.AddComponent(new ScreenBounds(_physics));

            Body body = BodyFactory.Instance.CreateCircleBody(_physics, 50f, 50f);
            Geom geom = GeomFactory.Instance.CreateCircleGeom(_physics, body, 50f, 20);

            /*Vertices verts = geom.LocalVertices;
            verts.SubDivideEdges(30);
            geom.SetVertices(verts);*/

            _physicalBlock = new PhysicsSprite(
                EuphoricEngine.Helpers.Drawing.DrawingHelper.CreateCircleTexture(Engine.GraphicsDevice, 50, 1, Color.Gold, Color.OrangeRed),
                new Vector2((float)Engine.GraphicsDevice.Viewport.Width - 100f, 51f),
                body,
                geom
            );
            this.AddComponent(_physicalBlock);

            body = BodyFactory.Instance.CreateRectangleBody(_physics, 600f, 50f, 100f);
            geom = GeomFactory.Instance.CreateRectangleGeom(_physics, body, 600f, 50f);

            PhysicsSprite sprite = new PhysicsSprite(
                EuphoricEngine.Helpers.Drawing.DrawingHelper.CreateRectangleTexture(Engine.GraphicsDevice, 600, 50, Color.Orange, Color.OrangeRed),
                new Vector2(((float)Engine.GraphicsDevice.Viewport.Width * 0.5f) + 100, 350f),
                body,
                geom
            );
            sprite.SetRotation(9f);
            sprite.IsStatic = true;
            this.AddComponent(sprite);
        }

        public override void Update()
        {
            _physics.Update(Engine.GameTime.ElapsedGameTime.Milliseconds * .001f);

            KeyboardState kbs = Keyboard.GetState();

            if (kbs.IsKeyDown(Keys.Escape))
                Engine.ShutDown();

            if (kbs.IsKeyDown(Keys.R))
            {
                _physicalBlock.SetRotation(0f);
                _physicalBlock.SetPosition(new Vector2((float)Engine.GraphicsDevice.Viewport.Width - 100f, 51f));
            }

            base.Update();
        }

        public override void Draw()
        {
            base.Draw();

            //
        }

        public PhysicsSimulator Physics
        {
            get { return _physics; }
        }
    }
}
